<?php
	
	$user = $controller->getCurrentUser();
	
	if ($user->isAllowedTo(GRADE_SEARCH_BOOK))
	{
		if ($request->getParam('element'))
		{
			$controller->setVar('user',$user);
			
			$elementsAllowed = array('book','serie','author','publisher');
			$element = (!in_array($request->getParam('element'), $elementsAllowed))?'book':$request->getParam('element');
			$sens = 'ASC';
			if ($request->getParam('sens') == 'ASC')            $sens = 'DESC';
			$link = ($request->getParam('link'))?$request->getParam('link'):'or';
			
			switch ($element)
			{
				case 'book' : 
					$orderDefault = 'al_name';
					$allowedTris = array('bo_cotation','al_name','al_tome', 'se_name','pu_name','bo_condition', 'bo_location', 'bo_mean');
					break;
				case 'serie' : 
					$orderDefault = 'se_name';
					$allowedTris = array('se_name','se_genre','se_stateBDBDEC','se_statePublisher','se_type','nb_al', 'se_mean');
					break;
				case 'author' : 
					$orderDefault = 'au_name';
					$allowedTris = array('au_name');
					break;
				case 'publisher' : 
					$orderDefault = 'pu_name';
					$allowedTris = array('pu_name');
					break;
				default : 
					$allowedTris = array();
					break;
			}
			$searchfields = $request->getParam('searchfields');
			$order = ($request->getParam('order'))?$request->getParam('order'):$orderDefault;
			
			$allowedField = array('al_name','se_name','pu_name','bo_cotation', 'au_name');
			if (!is_array($searchfields) || !array_in_array($searchfields, $allowedField))
				$searchfields = $allowedField;
				
			if (!in_array($order,$allowedTris))
			{
				$controller->addError('Un param&egrave;tre est erron&eacute; ! Veuillez utiliser les liens du menu pour naviguer dans le site.');
				$request->erase('element');
				$controller->forward('index');
			}
			else if (!$searchfield = $request->getParam('searchfield'))
			{
				$controller->addError('Vous n\'avez entr&eacute; aucun &eacute;lement &agrave; chercher !');
				$request->erase('element');
				$controller->forward('search');
			}
			else if (strlen($searchfield) < 3)
			{
				$controller->addError('Vous devez entrer au minimum 3 caract&egrave;res');
				$request->erase('element');
				$controller->forward('search');
			}
			else if (!$link || !in_array($link, array('or','and')))
			{
				$controller->addError('Un param&egrave;tre est erron&eacute; ! Veuillez utiliser les liens du menu pour naviguer dans le site.');
				$request->erase('element');
				$controller->forward('search');
			}
			else
			{
				//ici placer le truc de la recherche
				$results = call_user_func(array($element, 'search'), $element, $searchfield, $searchfields, $link, $order, $sens);
				if (!$results)
				{
					$controller->addInformation('La recherche n\'a retourn&eacute; aucun r&eacute;sultats');
					$request->erase('element');
					$controller->forward('search');
				}
				else
				{
					$controller->setVar('sens', $sens);
					$controller->setVar('searchfield', $searchfield);
					$controller->setVar('elementsList', $results);
					$controller->setVar('link', $link);
					$controller->setVar('action', 'search');
					$controller->render('displaySearch'.$element);
				}
			}
		}
		else
			$controller->render('search');
	}
	else
		$controller->render('errorRights');
  
  
?>
